﻿using System;
using System.Text;

namespace CommonLib
{
    partial class Tracking
    {
        TrackingDataClassesDataContext dt = new TrackingDataClassesDataContext();

        public Tracking(int vehicleID, DateTime timeStamp,
            decimal longitude, decimal latitude, float altitude,
            short heading, byte speed, int distanceKm,
            bool doorStatus, bool engineStatus, bool gpsStatus, bool gprsStatus, short currentFuel)
        {
            _vehicleID = vehicleID;
            _timeStamp = timeStamp;
            _longitude = longitude;
            _latitude = latitude;
            _altitude = altitude; //m unit
            _heading = heading;
            _speed = speed; //kmph unit
            _distanceKm = distanceKm; //km unit
            _doorStatus = doorStatus; //open-close state
            _engineStatus = engineStatus; //start-stop state
            _gpsStatus = gpsStatus; //ok
            _gprsStatus = gprsStatus; //ok
            _currentFuel = currentFuel; //lit
        }

        public void Insert(Tracking objTracking)
        {
            //Tracking localTracking = new Tracking()
            //{
            //    vehicleID = objTracking.vehicleID,
            //    timeStamp = objTracking.timeStamp,
            //    longitude = objTracking.longitude,
            //    latitude = objTracking.latitude,
            //    altitude = objTracking.altitude,
            //    heading = objTracking.heading,
            //    //address = objTracking.address,
            //    speed = objTracking.speed,
            //    distanceKm = objTracking.distanceKm,
            //    doorStatus = objTracking.doorStatus,
            //    engineStatus = objTracking.engineStatus,
            //    gpsStatus = objTracking.gpsStatus,
            //    gprsStatus = objTracking.gprsStatus,
            //    currentFuel = objTracking.currentFuel
            //};
            //dt.Trackings.InsertOnSubmit(localTracking);
            dt.Trackings.InsertOnSubmit(objTracking);
            dt.SubmitChanges();
        }

        /// <summary>
        /// Chuyển đổi các thông số tracking sang mảng byte
        /// </summary>
        /// <returns></returns>
        public byte[] ConvertObjectToByteArray()
        {
            int len = 0;
            byte[] data = new byte[51]; //51 bytes for the object

            //Id xe
            byte[] convert = BitConverter.GetBytes(_vehicleID); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //Thời gian
            convert = BitConverter.GetBytes(_timeStamp.Ticks); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //kinh độ
            convert = BitConverter.GetBytes((double)_longitude.Value); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //vĩ độ
            convert = BitConverter.GetBytes((double)_latitude.Value); //8 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //độ cao
            convert = BitConverter.GetBytes(_altitude.Value); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //hướng xe: 0-360
            convert = BitConverter.GetBytes(_heading.Value); //2 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //tốc độ
            convert = new byte[] { _speed.Value }; //1 byte
            convert.CopyTo(data, len);
            len += convert.Length;

            //quãng đường đi được
            convert = BitConverter.GetBytes(_distanceKm.Value); //4 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //trạng thái đóng/mở cửa
            convert = BitConverter.GetBytes(_doorStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //trạng thái tắt/mở máy
            convert = BitConverter.GetBytes(_engineStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //trạng thái gps
            convert = BitConverter.GetBytes(_gpsStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //trạng thái gprs
            convert = BitConverter.GetBytes(_gprsStatus.Value); //1 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            //trạng thái nhiên liệu
            convert = BitConverter.GetBytes(_currentFuel.Value); //2 bytes
            convert.CopyTo(data, len);
            len += convert.Length;

            return data;
        }

        /// <summary>
        /// Chuyển đổi mảng byte nhận được sang các thông số tracking
        /// </summary>
        /// <param name="data"></param>
        public void ConvertByteArrayToObject(byte[] data)
        {
            int len = 0;

            _vehicleID = BitConverter.ToInt32(data, 0); //4 bytes
            len += 4;

            _timeStamp = new DateTime(BitConverter.ToInt64(data, len)); //8 bytes
            len += 8;

            _longitude = (decimal)BitConverter.ToDouble(data, len); //8 bytes
            len += 8;

            _latitude = (decimal)BitConverter.ToDouble(data, len); //8 bytes
            len += 8;

            _altitude = BitConverter.ToSingle(data, len); //4 bytes
            len += 4;

            _heading = BitConverter.ToInt16(data, len); //2 bytes
            len += 2;

            _speed = data[len]; //1 bytes
            len++;

            _distanceKm = BitConverter.ToInt16(data, len); //4 bytes
            len += 4;

            _doorStatus = BitConverter.ToBoolean(data, len); //1 bytes
            len++;

            _engineStatus = BitConverter.ToBoolean(data, len); //1 bytes
            len++;

            _gpsStatus = BitConverter.ToBoolean(data, len); //1 bytes
            len++;

            _gprsStatus = BitConverter.ToBoolean(data, len); //1 bytes
            len++;

            _currentFuel = BitConverter.ToInt16(data, len); //4 bytes
            len += 4;
        }

        /// <summary>
        /// Dữ liệu mẫu
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public Tracking CreateSampleData(int deviceId, DateTime timestamp)
        {
            //Sample data
            Tracking objTracking = new Tracking(deviceId, timestamp,
                (decimal)10.827911, (decimal)106.627808, 100, 180, 90, 200,
                true, true, true, true, 90);
            return objTracking;
        }

        /// <summary>
        /// Dữ liệu mẫu
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public Tracking CreateSampleData(int deviceId)
        {
            //Sample data
            Tracking objTracking = new Tracking(deviceId, DateTime.Now,
                (decimal)10.827911, (decimal)106.627808, 100, 180, 90, 200,
                true, true, true, true, 90);
            return objTracking;
        }

        /// <summary>
        /// Dữ liệu mẫu
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public byte[] CreateSampleDataBytes(int deviceId)
        {
            //Sample data
            Tracking objTracking = CreateSampleData(deviceId);
            byte[] data = objTracking.ConvertObjectToByteArray();
            return data;
        }

        public byte[] CreateSampleDataBytes()
        {
            //Sample data
            Tracking objTracking = CreateSampleData(123);//default id = 123
            byte[] data = objTracking.ConvertObjectToByteArray();
            return data;
        }

    }
}
